<?php
namespace application\admin\panels;
use application\utilities\Utility;

class code_generator extends panel {
	public $name = 'Code Generator';
	
	public function title() {
		return 'Code Generator';
	}
	
	
	public function sidebar() {
		return array(
			array(
				'title'=>'Content Query',
				'sectionID'=>'content-query'
			),
			array(
				'title'=>'Users Query',
				'sectionID'=>'users-query'
			),
			array(
				'title'=>'Display Field',
				'sectionID'=>'display-field'
			),
			array(
				'title'=>'General Functions',
				'sectionID'=>'general-functions'
			)
		);
	}
	
	public function display() {
		?>
		
		
		<script type="text/javascript">
			$('.parameters').change(function() {
				var parameter = $(this).val();
				$('section:visible').find('.secondary-field').slideUp(200);
				$('section:visible').find('.secondary_' + parameter).slideDown(200);
				
			});
			$('.secondary-field').children('select.livesearch-multiple').livesearch('multiple');
			$('.secondary-field').children('select.livesearch').livesearch();
			
			$('.apply-parameter').click(function() {
				// Create the li for applied parameters list
				var li = $('<li/>').html('<b>' +$('section:visible').find('.parameters').find(':selected').html() + '</b>');
				var value = [];
				// Get values
				$('.secondary-field:visible').children('input[type="text"]').each(function() {
					if($(this).attr('name')) {
						value.push($(this).attr('name') + '=' + $(this).val());
					}
				});
				$('.secondary-field:visible').children('select').each(function() {
					if($(this).attr('name')) {
						var selectVal = [];
						$(this).find('option:selected').each(function() {
							selectVal.push($(this).val());
						});
						value.push($(this).attr('name') + '=' + selectVal.join(','));
					}
				});
				$('.secondary-field:visible').children('input[type="radio"]').each(function() {
					if($(this).attr('name')) {
						if($(this).attr('checked')) {
							value.push($(this).attr('name') + '=' + $(this).val());
						}
					}
				});
				$('.secondary-field:visible').children('input[type="checkbox"]').each(function() {	
					if($(this).attr('name')) {	
						if($(this).attr('checked')) {
							if($(this).attr('name').indexOf('[')) {
								var name = $(this).attr('name').replace('[]', '');
							}
							else {
								var name = $(this).attr('name');
							}
							
							if(value[name]) {
								var array = value[name].split(',');
								array.push($(this).val());
								value.push(name + '=' + array.join(','));
							}
							else {
								value.push(name + '=' + $(this).val());
							}	
							
						}
					}
				});
				li.data('parameters', value.join('&'));
				li.data('option', $('section:visible').find('.parameters').find(':selected'));
				

				li.append(' (' + value.join('&') + ')');
				$('<a/>').html('X').click(function(e) {
					e.preventDefault();
					var value = $(this).parent('li').data('value');
					var option = $(this).parent('li').data('option');
					option.attr('disabled', false);
					$(this).parent('li').remove();
				}).appendTo(li);
				li.appendTo($('section:visible').find('.applied-parameters'));
				
				$('section:visible').find('.parameters').find('option:selected').attr('disabled', 'disabled').attr('selected', false);
				$('.secondary-field').slideUp(200);
			});
		</script>
		<?php $content = DB()->getResults("SELECT `content_title`, `ct_title`, `content_id` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` ORDER BY `content_title`");?>
		<?php $contentTypes = DB()->getResults("SELECT `ct_plural`, `ct_title`, `ct_id` FROM `cn_content_types` ORDER BY `ct_title`");?>
		<?php $tags = DB()->getResults("SELECT `tag_title`, `tag_id`, `ct_plural` FROM `cn_tags` INNER JOIN `cn_content_types` ON `tag_content_type`=`ct_id` WHERE `tag_type` != 'group' ORDER BY `tag_title`");?>
		<?php $relations = DB()->getResults("SELECT * FROM `cn_content_relation_types` ORDER BY `crt_name`");?>
		<?php $users = DB()->getVarArray("SELECT `user_id` FROM `cn_users` ORDER BY `user_email`");?>
		<?php $interactions = DB()->getResults("SELECT `ci_id`, `ci_name`, `ct_plural` FROM `cn_content_interactions` INNER JOIN `cn_content_types` ON `ct_id`=`ci_content_type` ORDER BY `ci_name`");?>
		<?php $groups = DB()->getResults("SELECT `group_name`, `group_id` FROM `cn_user_groups` ORDER BY `group_name`");?>
		<section id="content-query">
			<div class="box">
			
				<h3>Generate Content Query</h3>
				<div class="box-content">
					<h5>Note that in order to display fields unique to a specific content type (ie, other than Title, Content, Parent, Timestamp, Status, ID, Author, or Permalink), you must provide a type declaration in your content query.</h5>
					<div class="two-column-left">
						<h4>Add Parameter</h4>
						<ul class="form-fields">
							
							<li>
								
								<select id="content-parameters" class="parameters">
									<option val="">Choose a Parameter</option>
									<optgroup label="General">
										<option value="id">Content is</option>
										<option value="not_id">Content is not</option>
										<option value="type">Content type is</option>
										<option value="status">Status is</option>
									</optgroup>
									<optgroup label="Tags">
										<option value="tags">Has tags</option>
										<option value="tag_operator">Tag operator</option>
									</optgroup>
									<optgroup label="Search">
										<option value="first_letter">First letter is</option>
										<option value="search">Title contains</option>
									</optgroup>
									<optgroup label="Content Relations">
										<option value="child_of">Child of</option>
										<option value="grandchild_of">Grandchild of</option>
										<option value="great_grandchild_of">Great-Grandchild of</option>
										<option value="content_relation">Has relation with</option>
									</optgroup>
									<optgroup label="Users">
										<option value="admin">Administrator is</option>
										<option value="interaction">Has interaction with user</option>
										<option value="author">Posted by</option>
									</optgroup>
									<optgroup label="Archiving">
										<option value="month">Posted in month</option>
										<option value="year">Posted in year</option>
										<option value="timestamp_greater_than">Posted after</option>
										<option value="timestamp_less_than">Posted before</option>
									</optgroup>
									<optgroup label="Order & Pagination">
										<option value="order">Order by</option>
										<option value="order_dir">Order direction</option>
										<option value="limit">Results per page</option>
										<option value="page">Show page</option>
									</optgroup>

								</select>
								<div class="secondary-field secondary_id">
									<select multiple="multiple" data-placeholder="Choose content(s)" class="livesearch-multiple" name="id">
										
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_not_id">
									<select multiple="multiple" name="not_id" data-placeholder="Choose content(s)" class="livesearch-multiple">
										
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_type">
									<h5>Choose content type</h5>
									<select name="type">
										<?php foreach($contentTypes as $type) : ?>
										<option value="<?=$type->ct_id?>"><?=$type->ct_title?></option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_status">
									<h5>Choose status</h5>
									<select name="status">
										<option value="">All</option>
										<option value="approved">Approved</option>
										<option value="pending">Pending</option>
										<option value="hidden">Hidden</option>
										<option value="stub">Stub</option>
									</select>
								</div>
								<div class="secondary-field secondary_order">
									<h5>Choose order column</h5>
									<select name="order">
										<option value="title">Title</option>
										<option value="timestamp">Date Posted</option>
										<option value="random">Random</option>
									</select>
								</div>
								<div class="secondary-field secondary_order_dir">
									<h5>Choose order direction</h5>
									<select name="order_dir">
										<option value="ASC">Ascending (A-Z, 0-9)</option>
										<option value="DESC">Descending (Z-A, 9-0)</option>
									</select>
								</div>
								<div class="secondary-field secondary_limit">
									<h5>Enter results per page</h5>
									<input type="text" class="thin" name="limit"/>
								</div>
								<div class="secondary-field secondary_page">
									<h5>Enter page #</h5>
									<p>Note that if the page number you provide is higher than the last page of results, then the last page will be displayed.</p>
									<input type="text" class="thin" name="page"/>
								</div>
								<div class="secondary-field secondary_tags">
									<select multiple="multiple" name="tags" data-placeholder="Choose tag(s)" class="livesearch-multiple">
										
										<?php foreach($tags as $tag) : ?>
										<option value="<?=$tag->tag_id?>"><?=$tag->tag_title?> (<?=$tag->ct_plural?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_tag_operator">
									<h5>Choose tag operator</h5>
									<select name="tag_operator">
										<option value="AND">AND</option>
										<option value="OR">OR</option>
									</select>
								</div>
								<div class="secondary-field secondary_month">
									<h5>Choose month</h5>
									<select name="month">
										<?php for($i=1;$i<=12;$i++) : ?>
										<option><?=$i?></option>
										<?php endfor;?>
									</select>
								</div>
								<div class="secondary-field secondary_year">
									<h5>Choose year</h5>
									<input type="text" class="thin" name="year"/>
								</div>
								<div class="secondary-field secondary_timestamp_greater_than">
									<h5>Enter DateTime</h5>
									<p>
										DateTime must be in format YYYY-MM-DD HH:MM:SS, where HH is the hour in 24-hour format (between 00 and 23)
									</p>
									<input type="text" name="timestamp_greater_than"/>
								</div>
								<div class="secondary-field secondary_timestamp_less_than">
									<h5>Enter DateTime</h5>
									<p>
										DateTime must be in format YYYY-MM-DD HH:MM:SS, where HH is the hour in 24-hour format (between 00 and 23)
									</p>
									<input type="text" name="timestamp_less_than"/>
								</div>
								
								<div class="secondary-field secondary_first_letter">
									<h5>Choose first letter</h5>
									<input type="text" name="first_letter" class="thin"/>
								</div>
								<div class="secondary-field secondary_search">
									<h5>Choose search text</h5>
									<input name="search" type="text"/>
								</div>
								<div class="secondary-field secondary_child_of">
									<select multiple="multiple" name="child_of" data-placeholder="Choose parent(s)" class="livesearch-multiple">
										
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_grandchild_of">
									<select multiple="multiple" name="grandchild_of" data-placeholder="Choose grandparent(s)" class="livesearch-multiple">
										
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_great_grandchild_of">
									<select multiple="multiple" name="great_grandchild_of" data-placeholder="Choose great-grandparent(s)" class="livesearch-multiple">
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_content_relation">
									<select name="content_relation">
										<option value="">Choose relation</option>
										<?php foreach($relations as $relation) : ?>
										<option value="<?=$relation->crt_id?>"><?=$relation->crt_name?></option>
										<?php endforeach;?>
									</select>
									<select name="content_relation_with" multiple="multiple" data-placeholder="With content(s)" class="livesearch-multiple">
										<?php foreach($content as $item) : ?>
										<option value="<?=$item->content_id?>"><?=$item->content_title?> (<?=$item->ct_title?>)</option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_admin">
									<select multiple="multiple" name="admin" data-placeholder="Choose administrator(s)" class="livesearch-multiple">
										<?php foreach($users as $user) : ?>
										<option value="<?=$user?>"><?=displayName($user)?></option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_author">
									<select multiple="multiple" name="author" data-placeholder="Choose author(s)" class="livesearch-multiple">
										<?php foreach($users as $user) : ?>
										<option value="<?=$user?>"><?=displayName($user)?></option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_interaction">
									<select name="interaction">
										<option value="">Choose interaction</option>
										<?php foreach($interactions as $interaction) : ?>
										<option value="<?=$interaction->ci_id?>"><?=$interaction->ci_name?> (<?=$interaction->ct_plural?>)</option>
										<?php endforeach;?>
									</select>
									<select name="interaction_user" multiple="multiple" data-placeholder="With user(s)" class="livesearch-multiple">
										<?php foreach($users as $user) : ?>
										<option value="<?=$user?>"><?=displayName($user)?></option>
										<?php endforeach;?>
									</select>
								</div>
							</li>
							<li>
								<button id="apply-content-parameter" class="small-button apply-parameter">Apply Parameter</button>
							</li>
						</ul>
					</div>
					<div class="two-column-right">
						<h4>Applied Parameters</h4>
						<ul class="applied-parameters">
						
						</ul>
					</div>
				</div>
			</div>
		</section>
		<section id="users-query" style="display:none;">
			<div class="box">
			
				<h3>Generate Users Query</h3>
				<div class="box-content">
					<div class="two-column-left">
						<h4>Add Parameter</h4>
						<ul class="form-fields">
							
							<li>
								
								<select id="content-parameters" class="parameters">
									<option val="">Choose a Parameter</option>
									<optgroup label="General">
										<option value="id">User is</option>
										<option value="not_id">User is not</option>
										<option value="level">User level is</option>
										<option value="group">User is in group</option>
									</optgroup>

								</select>
								<div class="secondary-field secondary_id">
									<select multiple="multiple" data-placeholder="Choose user(s)" class="livesearch-multiple" name="id">
										
										<?php foreach($users as $user) : ?>
										<option value="<?=$user?>"><?=displayName($user);?></option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_not_id">
									<select multiple="multiple" name="not_id" data-placeholder="Choose user(s)" class="livesearch-multiple">
										
										<?php foreach($users as $user) : ?>
										<option value="<?=$user?>"><?=displayName($user);?></option>
										<?php endforeach;?>
									</select>
								</div>
								<div class="secondary-field secondary_level">
									<h5>Choose level</h5>
									<select name="level">
										<option value="2">Global Administrator</option>
										<option value="1">Local Administrator</option>
										<option value="0">Normal User</option>
									</select>
								</div>
								<div class="secondary-field secondary_group">
									<h5>Choose group</h5>
									<select name="group">
										<option value="">Choose a group</option>
										<?php foreach($groups as $group) : ?>
										<option value="<?=$group->group_id?>"><?=$group->group_name?></option>
										<?php endforeach;?>
									</select>
								</div>
								
							</li>
							<li>
								<button id="apply-content-parameter" class="small-button apply-parameter">Apply Parameter</button>
							</li>
						</ul>
					</div>
					<div class="two-column-right">
						<h4>Applied Parameters</h4>
						<ul class="applied-parameters">
						
						</ul>
					</div>
				</div>
			</div>
		</section>
		<section id="general-functions" style="display:none;">
			<div class="box">
				<h3>Display Field</h3>
				<div class="box-content">
				
				</div>
			</div>
		</section>
		<?php
	}
}
?>